﻿using Newtonsoft.Json;

namespace Wechat.Corp.Message.Models
{
    /// <summary>
    /// 企业微信消息结构
    /// </summary>
    [JsonObject]
    public abstract class MessageRequest
    {
        #region json ignore

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string[] UserList { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string[] DeptList { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string[] TagList { get; set; }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("touser")]
        public string ToUser
        {
            get
            {
                if (UserList == null || UserList.Length <= 0)
                    return string.Empty;
                return string.Join("|", UserList);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("toparty")]
        public string ToParty
        {
            get
            {
                if (DeptList == null || DeptList.Length <= 0)
                    return string.Empty;
                return string.Join("|", DeptList);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("totag")]
        public string ToTag
        {
            get
            {
                if (TagList == null || TagList.Length <= 0)
                    return string.Empty;
                return string.Join("|", TagList);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("agentid")]
        public string AgentID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        abstract public string MessageType { get; }
        ///// <summary>
        ///// 
        ///// </summary>
        //[JsonProperty("safe")]
        //public byte Safe
        //{
        //    get;
        //    set;
        //} = byte.MinValue;
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("enable_duplicate_check")]
        public byte EnableDuplicateCheck
        {
            get;
            set;
        } = byte.MinValue;
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("duplicate_check_interval")]
        public ushort DuplicateCheckInterval
        {
            get;
            set;
        } = 1800;
    }
    /// <summary>
    /// 文本消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "text",
    ///    "agentid" : 1,
    ///    "text" : {
    ///        "content" : "你的快递已到，请携带工卡前往邮件中心领取。\n出发前可查看<a href="https://work.weixin.qq.com">邮件中心视频实况</a>，聪明避开排队。"
    ///    },
    ///    "safe":0,
    ///    "enable_id_trans": 0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class TextMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "text"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Content
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("text")]
        public object Text
        {
            get
            {
                return new { content = this.Content };
            }
        }
    }
    /// <summary>
    /// 图片消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "image",
    ///    "agentid" : 1,
    ///    "image" : {
    ///         "media_id" : "MEDIA_ID"
    ///    },
    ///    "safe":0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class ImageMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "image"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string MediaId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("image")]
        public object Image
        {
            get
            {
                return new { media_id = this.MediaId };
            }
        }
    }
    /// <summary>
    /// 语音消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "voice",
    ///    "agentid" : 1,
    ///    "voice" : {
    ///         "media_id" : "MEDIA_ID"
    ///    },
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class VoiceMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "voice"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string MediaId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("voice")]
        public object Voice
        {
            get
            {
                return new { media_id = this.MediaId };
            }
        }
    }
    /// <summary>
    /// 视频消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "video",
    ///    "agentid" : 1,
    ///    "video" : {
    ///         "media_id" : "MEDIA_ID",
    ///         "title" : "Title",
    ///         "description" : "Description"
    ///    },
    ///    "safe":0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class VideoMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "video"; }
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string MediaId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Title { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Description { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("video")]
        public object Video
        {
            get
            {
                return new { media_id = MediaId, title = Title, description = Description };
            }
        }
    }
    /// <summary>
    /// 文件消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "file",
    ///    "agentid" : 1,
    ///    "file" : {
    ///         "media_id" : "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o"
    ///    },
    ///    "safe":0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class FileMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "file"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string MediaId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("file")]
        public object File
        {
            get
            {
                return new { media_id = this.MediaId };
            }
        }
    }
    /// <summary>
    /// 文本卡片消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1 | PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "textcard",
    ///    "agentid" : 1,
    ///    "textcard" : {
    ///         "title" : "领奖通知",
    ///         "description" : "<div class="gray">2016年9月26日</div> <div class="normal">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class="highlight">请于2016年10月10日前联系行政同事领取</div>",
    ///         "url" : "URL",
    ///         "btntxt":"更多"
    ///    },
    ///    "enable_id_trans": 0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class TextCardMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "textcard"; }
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Title { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Url { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string BtnText { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("textcard")]
        public object TextCard
        {
            get
            {
                return new
                {
                    title = Title,
                    description = Description,
                    url = Url,
                    btntxt = BtnText
                };
            }
        }
    }
    /// <summary>
    /// 图文消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1 | PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype" : "news",
    ///    "agentid" : 1,
    ///    "news" : {
    ///        "articles" : [
    ///            {
    ///                "title" : "中秋节礼品领取",
    ///                "description" : "今年中秋节公司有豪礼相送",
    ///                "url" : "URL",
    ///                "picurl" : "https://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", 
	///	               "appid": "wx123123123123123",
    /// 	           "pagepath": "pages/index?userid=zhangsan&amp;orderid=123123123"
    ///            }
    ///         ]
    ///    },
    ///    "enable_id_trans": 0,
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class NewsMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public NewsMessageRequest()
        {
            this.Articles = [];
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "news"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public List<NewsArticle> Articles
        {
            get;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("news")]
        public object News
        {
            get
            {
                return new
                {
                    articles = this.Articles
                };
            }
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class NewsArticle
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("title")]
        public string Title { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("picurl")]
        public string PictureUrl { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("url")]
        public string Url { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("description")]
        public string Description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("appid")]
        public string? AppId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("pagepath")]
        public string? PagePath { get; set; }
    }

    /// <summary>
    /// markdown消息
    /// {
    ///    "touser" : "UserID1|UserID2|UserID3",
    ///    "toparty" : "PartyID1|PartyID2",
    ///    "totag" : "TagID1 | TagID2",
    ///    "msgtype": "markdown",
    ///    "agentid" : 1,
    ///    "markdown": {
    ///         "content": "您的会议室已经预定，稍后会同步到`邮箱`  
    ///                     <br/>>**事项详情**  
    ///                     <br/>>事　项：<font color="info">开会</font>  
    ///                     <br/>>组织者：@miglioguan  
    ///                     <br/>>参与者：@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang  
    ///                     <br/>>  
    ///                     <br/>>会议室：<font color="info">广州TIT 1楼 301</font>  
    ///                     <br/>>日　期：<font color="warning">2018年5月18日</font>  
    ///                     <br/>>时　间：<font color="comment">上午9:00-11:00</font>  
    ///                     <br/>>  
    ///                     <br/>>请准时参加会议。  
    ///                     <br/>>  
    ///                     <br/>>如需修改会议信息，请点击：[修改会议信息](https://work.weixin.qq.com)"
    ///    },
    ///    "enable_duplicate_check": 0,
    ///    "duplicate_check_interval": 1800
    /// }
    /// </summary>
    public class MarkdownMessageRequest : MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "markdown"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Content
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("markdown")]
        public object Markdown
        {
            get
            {
                return new { content = this.Content };
            }
        }
    }


}
